import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;

/**
 * The client program.
 * 
 * @author jhaberstro
 */
public class HexClient
{
	/**
	 * The main method.
	 * @param args - first argument should be the host, the second argument should be the port, and the third argument should be the session.
	 */
	public static void main(String[] args) {
		if (args.length != 3) {
			printArgumentsError();
		}
		
		String host = args[0];
		int port = -1;
		try {
			port = Integer.parseInt(args[1]);
		}
		catch (NumberFormatException exception) {
			printArgumentsError();
		}
		
		Socket socket = new Socket();
		try {
			socket.connect(new InetSocketAddress(host, port));
		}
		catch (IOException exception) {
			System.err.println("Unable to establish connection to given host and port.");
			System.exit(1);
		}
		
		HexUI ui = new HexUI();
		ModelProxy modelProxy = new ModelProxy(socket);
		ui.setViewListener(modelProxy);
		modelProxy.setModelListener(ui);
		ui.setVisible(true);
		modelProxy.join(null,args[2]);
	}
	
	/**
	 * Print the usage and exit.
	 */
	public static void printArgumentsError() {
		System.err.println("Invalid arguments.\nPlease run as: java HexClient <host> <port> <session>");
		System.exit(1);
	}
}
